import re




ui_class_name = "AxisLimitConfig"
config_instance_func_name = "axis_limits_config_instance"

params = """self._x_limit_neg = 0
        self._x_limit_pos = 0
        self._y_limit_neg = 0
        self._y_limit_pos = 0
        self._z_limit_neg = 0
        self._z_limit_pos = 0
        self._a_limit_neg = 0
        self._a_limit_pos = 0
        self._b_limit_neg = 0
        self._b_limit_pos = 0"""

regex = r"_([a-zA-Z_0-9]+)\s*="
matches = re.finditer(regex, params)

params_list = []
for match in matches:
    params_list.append(match.group(1))
print(params_list)

print("\r")
print("----------生成init_with_json函数中的内容--------------------")
# 生成init_with_json函数中的内容
temp_1 = "self.{} = data[\"{}\"]"
results_1 = []
for p in params_list:
    results_1.append(temp_1.format("_" + p, p))
str_1 = "\r".join(results_1)
print(str_1)

print("\r")

print("----------生成build_data_dict函数中的内容--------------------")
# 生成build_data_dict函数中的内容
temp_2 = "    \"{}\": self.{},"
results_2 = []
for p in params_list:
    results_2.append(temp_2.format(p, "_" + p))
str_2 = "\r".join(results_2)
print("{\r" + str_2 + "\r}\r")

print("\r")

print("----------生成init_ui函数的内容--------------------")
# 生成init_ui函数的内容
head_str = "from comm_modules.parameter.api import {}\r\r_C = {}()".format(config_instance_func_name, config_instance_func_name)
temp_3 = "{} = _C.read_data(\"{}\")"
temp_3_ui = "self.ui.lineEdit_{}.setText(\"%f\" % {})"

results_3 = []
for p in params_list:
    results_3.append(temp_3.format("_" + p, p))
results_3.append("\r")

for p in params_list:
    results_3.append(temp_3_ui.format(p, "_" + p))
    
str_3 = "\r".join(results_3)
print(head_str + "\r" + str_3)

print("\r")
print("----------生成apply的内容--------------------")
# 生成apply的内容
temp_4 = "    _{} = float(self.ui.lineEdit_{}.text())"
temp_4_2 = "_C.write_data(\"{}\", _{})"
results_4 = ["try:"]
for p in params_list:
    results_4.append(temp_4.format(p, p)) 
results_4.append("except Exception as e:")
results_4.append("    _err_format = _translate(\"{}\", \"please input valid double value!!!\")".format(ui_class_name))
results_4.append("    _ERROR(_err_format)")
results_4.append("    return")

for p in params_list:
    results_4.append(temp_4_2.format(p, p))
    
results_4.append("_C.apply_config()")

str_4 = "\r".join(results_4)
print(str_4)

print("\r")
print("-------------生成config_ui函数的内容-----------")
# 生成config_ui函数的内容
results_5 = ["rx = QtCore.QRegExp(\"[0-9\.\-]+$\")", "validator = QtGui.QRegExpValidator(rx, self)"]
temp_5 = "self.ui.lineEdit_{}.setValidator(validator)"

for p in params_list:
    results_5.append(temp_5.format(p))

str_5 = "\r".join(results_5)
print(str_5)


